/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientListener;

import appClient.ClientCommand;
import appClient.ConnectClient;
import appClient.Forms;
import appClient.UserComman;
import com.google.gson.Gson;
import engine.AdressBooks;
import engine.FullPersone;
import engine.Persone;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import com.google.gson.reflect.TypeToken;
import engine.City;
import engine.Country;
import engine.GType;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import protocol.ClientProtocol;
import protocol.Command;

/**
 *
 * @author User
 */
public class AppClientListener implements ClientListener {
    OutputStream os;
  
  public String setRole(ClientEvent evn) {
      String status = "";
    ConnectClient connectClient = evn.connectClient;
    String role = evn.data;
    String add = evn.add;
    if (role.equals("Error")){
        status = "Error";
      System.out.println("not login");
    }
    try {
        if (role.equals("Admin")){
          connectClient.role = "Admin";
          connectClient.vis.makeVisible();
          connectClient.Uid = add;
          UserComman.getList(connectClient);
          status = "Admin";
        }
    } catch (Exception exp)
      {
          status = "Admin";
        System.out.println("No Server");
      }
    try {
        if (role.equals("SUser")){
          connectClient.role = "SUser";
          connectClient.vis.makeVisible();
          connectClient.Uid = add;
          UserComman.getList(connectClient);
          status = "SUser";
        }
      } catch (Exception exp)
      {
          status = "SUser";
        System.out.println("No Server");
      }
    try {
        if (role.equals("User")){
          connectClient.role = "User";
          connectClient.vis.makeInvisible();
          connectClient.Uid = add;
          UserComman.getList(connectClient);
          status = "User";
        }
      } catch (Exception exp)
      {
          status = "User";
        System.out.println("No Server");
      }
    return status;
  }
  
  public boolean setList(ClientEvent evn)
  {
    ConnectClient connectClient = evn.connectClient;
    String add = evn.add;
    Gson gson = new Gson();
    Type fooType = new TypeToken<List<Persone>>() {}.getType();
    List<Persone> list = gson.fromJson(add, fooType);
    String phone[][] = new String[list.size()][3];
    int count = 0;
    for (Persone p : list) {
      phone[count][0] = p.ID;
      phone[count][1] = p.FName;
      phone[count][2] = p.LNmae;
      count++;
    }
    try {
        connectClient.vis.TableContact = new JTable();
        connectClient.vis.TableContact.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        connectClient.vis.TableContact.setModel(new javax.swing.table.DefaultTableModel(phone, new String[]{"ID", "Имя", "Фамилия"}));
        connectClient.vis.ScrollContact.setViewportView(connectClient.vis.TableContact);
    } catch (Exception exp)
      {
        System.out.println("No Server");
      }
    return true;
  }
  
  public boolean loadPersone(ClientEvent evn) {
        try {
            FullPersone fp = evn.fp;
            ClientProtocol cp = evn.cp;
            ConnectClient connectClient = evn.connectClient;
                cp.ab = new AdressBooks(fp, cp);
                ClientCommand.loadPT(cp, connectClient);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AppClientListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AppClientListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
  
  public boolean typePhone(ClientEvent evn) {
    Forms ab = evn.ab;
    String types = evn.data;
        Gson gson = new Gson();       
        Type fooType = new TypeToken<List<TypePhone>>() {}.getType();
        List<TypePhone> list = gson.fromJson(types, fooType);
        
        String phoneT[] = new String[list.size()];
        int count = 0;
        for (TypePhone p : list) {
            phoneT[count] = p.Type;
            count++;
        }
        ab.setItems(phoneT);
        ab.phoneTable();

        return true;
    }
  
  public boolean typeAdr(ClientEvent evn) {
    Forms ab = evn.ab;
    String add = evn.data;
        Gson gson = new Gson();       
        Type fooType = new TypeToken<List<TypeAdress>>() {}.getType();
        List<TypeAdress> list = gson.fromJson(add, fooType);
        
        String phoneT[] = new String[list.size()];
        int count = 0;
        for (TypeAdress p : list) {
            phoneT[count] = p.Type;
            count++;
        }
        ab.setAdrType(phoneT);
        ab.AdrTable();
        
        return true;
    }
  
  public boolean country(ClientEvent evn) {
    Forms ab = evn.ab;
    String add = evn.data;
        Gson gson = new Gson();       
        Type fooType = new TypeToken<List<Country>>() {}.getType();
        List<Country> list = gson.fromJson(add, fooType);
        
        String phoneT[] = new String[list.size()];
        int count = 0;
        for (Country p : list) {
            phoneT[count] = p.name;
            count++;
        }
        ab.setCountr(phoneT);
        ab.setCountryList(list);
        ab.AdrTable();
        
        return true;
    }
  
  public boolean city(ClientEvent evn) {
    Forms ab = evn.ab;
    String add = evn.data;
        Gson gson = new Gson();       
        Type fooType = new TypeToken<List<City>>() {}.getType();
        List<City> list = gson.fromJson(add, fooType);
        
        String phoneT[] = new String[list.size()];
        int count = 0;
        for (City p : list) {
            phoneT[count] = p.name;
            count++;
        }
        ab.setCit(phoneT);
        ab.setCityList(list);
        ab.setTableAdr();
       // ab.AdrTable();
        
        return true;
    }
  
  public boolean street(ClientEvent evn) {
    Forms ab = evn.ab;
    String add = evn.data;
        Gson gson = new Gson();       
        Type fooType = new TypeToken<List<Street>>() {}.getType();
        List<Street> list = gson.fromJson(add, fooType);
        
        String phoneT[] = new String[list.size()];
        int count = 0;
        for (Street p : list) {
            phoneT[count] = p.name;
            count++;
        }
        ab.setStre(phoneT);
       // ab.cityList = list;
        ab.setTableAdr();
       // ab.AdrTable();
        
        return true;
    }

    @Override
    public boolean foto(ClientEvent clientEvent) {
    try {
      Forms ab = clientEvent.ab;
      String add = clientEvent.data;
      os = new BufferedOutputStream(new FileOutputStream("src/foto/buf.jpg"));
      Gson gson = new Gson();
      int cbuf[] = gson.fromJson(add, int[].class);
      for(int i : cbuf){
        os.write(i);
      }
      os.close();
      ab.setPhoto();
      
    } catch (IOException ex) {
      Logger.getLogger(AppClientListener.class.getName()).log(Level.SEVERE, null, ex);
    } 
    return true;
        }
        
    }
  

